package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Address;
import com.atguigu.service.AddressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Reference
    AddressService addressService;

    @RequestMapping("/findAllMaps")
    public Map findAllMaps() {
        try {
            Map<String, List<Map<String, String>>> map = new HashMap();

            List<Address> addressList = addressService.findAllMaps();
            List<Map<String, String>> gridnMaps = new ArrayList<>();
            List<Map<String, String>> nameMaps = new ArrayList<>();
            for (Address address : addressList) {
                Map<String, String> gm = new HashMap<>();
                gm.put("lng", address.getLng());
                gm.put("lat", address.getLat());

                Map<String, String> nm = new HashMap<>();
                nm.put("addressName", address.getAddressName());

                gridnMaps.add(gm);
                nameMaps.add(nm);
            }
            map.put("gridnMaps", gridnMaps);
            map.put("nameMaps", nameMaps);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap();
        }
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = addressService.findPage(queryPageBean);
            return new Result(true, MessageConstant.GET_ADDRESS_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ADDRESS_FAIL);
        }
    }
    @RequestMapping("/addAddress")
    public Result addAddress(@RequestBody Address address) {
        try {
            addressService.addAddress(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }
    @RequestMapping("/deleteById")
    public Result deleteById(String addressId) {
        try {
            addressService.deleteById(addressId);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }


}
